﻿using Microsoft.AspNetCore.Mvc;
using PermissionDesign.Model;
using PermissionDesign.Model.Response;
using PermissionDesign.Service;
using PermissionDesign.Tool;
using System;
using System.Web;

namespace PermissionDesign.Web.Controllers
{
    public class LoginController : Controller
    {
        private LoginService _loginService;
        private RoleService _roleService;
        public LoginController(LoginService loginService, RoleService roleService)
        {
            _loginService = loginService;
            _roleService = roleService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Login()
        {
            string username = Request.Form["username"];
            string password = Request.Form["password"];
            string j_remember = Request.Form["j_remember"];

            var exist = _loginService.IsUserExist(c => c.LoginAccount == username);
            if (!exist)
            {
                return Json(new { result = false, data = "用户不存在" });
            }

            var loginResult = _loginService.Login(username, password);
            if (loginResult.code == 200)
            {
                var user = loginResult.data as Response_User;

                //处理cookie
                if (j_remember == "true")
                {
                    CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginCookieKey), username + "^" + password, DateTime.Now.AddDays(7), Request, Response);
                }
                else
                {
                    CookieHelper.SingleTon().ClearCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginCookieKey), Request, Response);
                }

                //是否正在登录
                CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.IsLoginingCookieKey), "1", Request, Response);

                //用户账号
                CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingUserAccountKey), username, Request, Response);

                //角色id
                CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingRoleIdKey), user.RoleId.ToString(), Request, Response);

                //用户姓名
                CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingNameKey), user.Name, Request, Response);

                return Json(new { result = true, data = "登录成功" });
            }
            else
            {
                CookieHelper.SingleTon().ClearCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginCookieKey), Request, Response);
                CookieHelper.SingleTon().ClearCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingUserAccountKey), Request, Response);
                CookieHelper.SingleTon().ClearCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingRoleIdKey), Request, Response);
                CookieHelper.SingleTon().ClearCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingNameKey), Request, Response);
                //是否正在登录
                CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.IsLoginingCookieKey), "0", Request, Response);

                return Json(new { result = false, data = loginResult.result });
            }
        }


        /// <summary>
        /// 注销登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult LoginOff()
        {
            //是否正在登录
            CookieHelper.SingleTon().SetCookie(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.IsLoginingCookieKey), "0", Request, Response);
            return Json("true");
        }


        /// <summary>
        /// 获取cookie中的用户账号和密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetCookieUser()
        {
            string strCookie = CookieHelper.SingleTon().GetCookieValue(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginCookieKey), Request);

            if (!string.IsNullOrEmpty(strCookie))
            {
                string[] arr = strCookie.Split("^");
                return Json(new { name = arr[0], passwd = arr[1] });
            }
            return Json(new { name = "", passwd = "" });
        }


        /// <summary>
        /// 判断是否已经登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string GetIsLogin()
        {
            //是否正在登录(0:未登录 1:已登录)
            return CookieHelper.SingleTon().GetCookieValue(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.IsLoginingCookieKey), Request);
        }


        /// <summary>
        /// 获取cookie中的角色id
        /// </summary>
        /// <returns></returns>
        public string GetCookieRoleId()
        {
            string roleId = CookieHelper.SingleTon().GetCookieValue(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingRoleIdKey), Request);
            return roleId;
        }

        /// <summary>
        /// 获取当前用户的角色编码
        /// </summary>
        public string GetRoleEncoding(int roleId)
        {
            if (roleId <= 0)
            {
                return "";
            }
            else
            {
                var result = _roleService.GetOneRole(c => c.Id == roleId);
                if (result.code == 200)
                {
                    return result.data.RoleEncoding;
                }
                else
                {
                    return "";
                }
            }
        }


        /// <summary>
        /// 获取当前用户名称
        /// </summary>
        public string GetCookieUserName()
        {
            string userName = HttpUtility.UrlDecode(CookieHelper.SingleTon().GetCookieValue(EnumHelper.SingleTon().GetEnumDescription(EnumConfig.LoginingNameKey), Request));
            return userName;
        }

    }
}